//
//  CYQiYeRuZhuViewController.m
//  cyydt
//
//  Created by chu on 2019/2/6.
//  Copyright © 2019 chu. All rights reserved.
//

#import "CYQiYeRuZhuViewController.h"
#import "CYAddressModel.h"
#import "CYMinePickerView.h"
#import "CYSelCategaryView.h"

@interface CYQiYeRuZhuViewController ()<UITextFieldDelegate>
{
    NSString *_provinceId;
    NSString *_cityId;
    NSNumber *_cateId;
}
@property (weak, nonatomic) IBOutlet LPButton *btn1;
@property (weak, nonatomic) IBOutlet LPButton *btn2;
@property (weak, nonatomic) IBOutlet LPButton *btn3;
@property (weak, nonatomic) IBOutlet LPButton *btn4;
@property (weak, nonatomic) IBOutlet UIView *backView;
@property (weak, nonatomic) IBOutlet UITextField *companyNameTF;
@property (weak, nonatomic) IBOutlet UITextField *quanchengTF;
@property (weak, nonatomic) IBOutlet UITextField *addressTF;
@property (weak, nonatomic) IBOutlet UITextField *hangteTF;
@property (weak, nonatomic) IBOutlet UITextField *lianxirenTF;
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UITextField *wechatTF;
@property (weak, nonatomic) IBOutlet UIButton *doneBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *backViewHeightConstraint;
@property (nonatomic, strong) NSMutableArray *addressArr;

@property (nonatomic, strong) NSMutableArray *categaryArr;
@end

@implementation CYQiYeRuZhuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"企业入驻";
    [self getAddress];
    [self getAllCategary];
    [self initLayer];
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    if (textField == self.addressTF) {
        [self showAddress];
        return NO;
    }
    if (textField == self.hangteTF) {
        [self selHangYe];
        return NO;
    }
    return YES;
}

- (void)updateViewConstraints{
    [super updateViewConstraints];
    self.backViewHeightConstraint.constant = CGRectGetMaxY(self.doneBtn.frame) + 20;
}

- (void)initLayer{
    self.doneBtn.layer.cornerRadius = self.doneBtn.frame.size.height / 2;
    self.doneBtn.layer.masksToBounds = YES;
    
    self.backView.layer.cornerRadius = 5;
    self.backView.layer.masksToBounds = YES;
    
    self.companyNameTF.delegate = self;
    self.quanchengTF.delegate = self;
    self.addressTF.delegate = self;
    self.hangteTF.delegate = self;
    self.lianxirenTF.delegate = self;
    self.phoneTF.delegate = self;
    self.wechatTF.delegate = self;

    self.btn1.style = LPButtonStyleTop;
    self.btn2.style = LPButtonStyleTop;
    self.btn3.style = LPButtonStyleTop;
    self.btn4.style = LPButtonStyleTop;

}

- (IBAction)doneAction:(UIButton *)sender {
    if ([NSString stringIsNull:self.companyNameTF.text]) {
        [self.view makeToast:@"企业名称不能为空" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    if ([NSString stringIsNull:self.quanchengTF.text]) {
        [self.view makeToast:@"企业名称不能为空" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    if ([NSString stringIsNull:_provinceId]) {
        [self.view makeToast:@"所属地区不能为空" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    if (!_cateId) {
        [self.view makeToast:@"所属行业不能为空" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    if ([NSString stringIsNull:self.lianxirenTF.text]) {
        [self.view makeToast:@"联系人不能为空" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    if ([NSString stringIsNull:self.phoneTF.text]) {
        [self.view makeToast:@"联系电话不能为空" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    if ([NSString stringIsNull:self.wechatTF.text]) {
        [self.view makeToast:@"联系微信不能为空" duration:1.5 position:CSToastPositionCenter];
        return;
    }
    
    [EasyShowLodingView showLoding];
    NSString *url = [NSString stringWithFormat:@"%@%@",User_HOST, @"company"];
    NSDictionary *param = @{@"name":self.companyNameTF.text, @"fullName":self.quanchengTF.text, @"province":[NSNumber numberWithInteger:[_provinceId integerValue]], @"city":[NSNumber numberWithInteger:[_cityId integerValue]], @"cateId":[_cateId stringValue], @"contacts":self.lianxirenTF.text, @"phone":self.phoneTF.text, @"wechat":self.wechatTF.text};
    [[XBRequest sharedInstance] postDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        NSLog(@"我是创业者 ---- %@",responseResult);
        [EasyShowLodingView hidenLoding];
        if ([responseResult objectForKey:@"resError"]) {
            [self.view makeToast:ResErrorMessage];
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                [[UIApplication sharedApplication].keyWindow makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
                [[AppDelegate sharedAppDelegate] popViewController];
            }else{
                [self.view makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
            }
        }
    }];
}

- (IBAction)btnClick:(LPButton *)sender {
    
}

#pragma mark - 地址选择
- (void)showAddress{
    CYMinePickerView *headView = [[CYMinePickerView alloc] initWithFrame:CGRectMake(0, 0, kWindowW, 305) DataArr:self.addressArr components:2];
    headView.titleLable.text = @"请选择地区";
    headView.addressBlock = ^(CYAddressModel * _Nonnull province, CYCityModel * _Nonnull city) {
        NSLog(@"province.name -- %@",province.name);
        NSLog(@"city -- %@",city.name);
        self.addressTF.text = [NSString stringWithFormat:@"%@-%@",province.name, city.name];
        self->_provinceId = province.areaid;
        self->_cityId = city.areaid;
    };
    [GKCover translucentCoverFrom:[UIApplication sharedApplication].keyWindow content:headView animated:YES showBlock:^{
        // 显示出来时的block
        NSLog(@"弹窗显示了，6不6");
    } hideBlock:^{
        // 移除后的block
        NSLog(@"弹窗消失了，555");
    }];
}

#pragma mark - 选择感兴趣的行业
- (void)selHangYe{
    NSArray *nibView =  [[NSBundle mainBundle] loadNibNamed:@"CYSelCategaryView" owner:self options:nil];
    CYSelCategaryView *backView = [nibView objectAtIndex:0];
    backView.frame = CGRectMake(0, 0, kWindowW, 410);
    backView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
    backView.dataSourceArr = self.categaryArr;
    backView.block = ^(CYCategrayModel * _Nonnull bigCateModel, CYCategrayModel * _Nonnull smallCateModel) {
        self->_cateId = bigCateModel.catid;
        self.hangteTF.text = bigCateModel.catname;
    };
    [GKCover translucentCoverFrom:[UIApplication sharedApplication].keyWindow content:backView animated:YES showBlock:^{
        // 显示出来时的block
        NSLog(@"弹窗显示了，6不6");
    } hideBlock:^{
        // 移除后的block
        NSLog(@"弹窗消失了，555");
    }];
}

#pragma mark - 获取地址
- (void)getAddress{
    NSString *url = [NSString stringWithFormat:@"%@%@",User_HOST, @"areas"];
    [[XBRequest sharedInstance] getDataWithUrl:url Parameter:nil ResponseObject:^(NSDictionary *responseResult) {
        NSLog(@"获取地址 ---- %@",responseResult);
        [EasyShowLodingView hidenLoding];
        if ([responseResult objectForKey:@"resError"]) {
            
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                if ([responseResult[@"data"] isKindOfClass:[NSArray class]]) {
                    NSArray *data = responseResult[@"data"];
                    [self.addressArr removeAllObjects];
                    for (NSDictionary *dic in data) {
                        CYAddressModel *model = [CYAddressModel mj_objectWithKeyValues:dic];
                        [self.addressArr addObject:model];
                    }
                }
            }else{
                
            }
        }
    }];
}

#pragma mark - 获取所有分类
- (void)getAllCategary{
    NSString *url = [NSString stringWithFormat:@"%@%@",User_HOST, @"categories"];
    NSDictionary *param = @{@"type":@3};
    [[XBRequest sharedInstance] getDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        NSLog(@"获取所有分类 ---- %@",responseResult);
        [EasyShowLodingView hidenLoding];
        if ([responseResult objectForKey:@"resError"]) {
            
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                if ([responseResult[@"data"] isKindOfClass:[NSArray class]]) {
                    NSArray *data = responseResult[@"data"];
                    [self.categaryArr removeAllObjects];
                    for (NSDictionary *dic in data) {
                        NSMutableDictionary *muDic = [NSMutableDictionary dictionaryWithDictionary:dic];
                        [muDic setObject:dic[@"name"] forKey:@"catname"];
                        [muDic setObject:dic[@"id"] forKey:@"catid"];
                        [muDic setObject:dic[@"childCates"] forKey:@"child"];
                        [muDic removeObjectForKey:@"id"];
                        [muDic removeObjectForKey:@"name"];
                        [muDic removeObjectForKey:@"childCates"];
                        CYCategrayModel *model = [CYCategrayModel modelWithDictionary:muDic];
                        [self.categaryArr addObject:model];
                    }
                }
            }else{
                
            }
        }
    }];
}

- (NSMutableArray *)addressArr{
    if (!_addressArr) {
        _addressArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _addressArr;
}

- (NSMutableArray *)categaryArr{
    if (!_categaryArr) {
        _categaryArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _categaryArr;
}

@end
